﻿#include "TucsonEgg.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/ServerPackets.h"
#include "../../Settings.h"
#include "../MapObject.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool TucsonEgg::getCanMove() const
    {
        return false;
    }

    TucsonEgg::TucsonEgg(MonsterInfo *info) : MonsterObject(info)
    {
    }

    void TucsonEgg::Turn(MirDirection dir)
    {
    }

    bool TucsonEgg::Walk(MirDirection dir)
    {
        return false;
    }

    bool TucsonEgg::IsAttackTarget(MonsterObject *attacker)
    {
        return MonsterObject::IsAttackTarget(attacker);
    }

    bool TucsonEgg::IsAttackTarget(PlayerObject *attacker)
    {
        return MonsterObject::IsAttackTarget(attacker);
    }

    void TucsonEgg::ProcessRoam()
    {
    }

    void TucsonEgg::ProcessSearch()
    {
        MonsterObject::ProcessSearch();
    }

    Packet *TucsonEgg::GetInfo()
    {
        return MonsterObject::GetInfo();
    }

    int TucsonEgg::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        int armour = 0;

        switch (type)
        {
            case DefenceType::ACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetDefencePower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::AC:
                armour = GetDefencePower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::MACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetDefencePower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::MAC:
                armour = GetDefencePower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::Agility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                break;
        }

        if (armour >= damage)
        {
            return 0;
        }

        ShockTime = 0;

        for (int i = PoisonList.size() - 1; i >= 0; i--)
        {
            if (PoisonList[i]->PType != PoisonType::LRParalysis)
            {
                continue;
            }

            PoisonList.erase(PoisonList.begin() + i);
            OperateTime = 0;
        }

        if (attacker->Info->AI == 6)
        {
            EXPOwner = nullptr;
        }
        else if (attacker->Master != nullptr)
        {
            if (EXPOwner == nullptr || EXPOwner->Dead)
            {
                EXPOwner = attacker->Master;
            }

            if (EXPOwner == attacker->Master)
            {
                EXPOwnerTime = getEnvir()->getTime() + EXPOwnerDelay;
            }

        }

        S::ObjectStruck *tempVar = new S::ObjectStruck();
        tempVar->ObjectID = ObjectID;
        tempVar->AttackerID = attacker->ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);

        ChangeHP(-1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        return 1;
    }

    int TucsonEgg::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        int armour = 0;

        switch (type)
        {
            case DefenceType::ACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetDefencePower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::AC:
                armour = GetDefencePower(Stats[Stat::MinAC], Stats[Stat::MaxAC]);
                break;
            case DefenceType::MACAgility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                armour = GetDefencePower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::MAC:
                armour = GetDefencePower(Stats[Stat::MinMAC], Stats[Stat::MaxMAC]);
                break;
            case DefenceType::Agility:
                if (getEnvir()->Random->Next(Stats[Stat::Agility] + 1) > attacker->Stats[Stat::Accuracy])
                {
                    return 0;
                }
                break;
        }

        if (armour >= damage)
        {
            return 0;
        }

        if (damageWeapon)
        {
            attacker->DamageWeapon();
        }

        ShockTime = 0;

        for (int i = PoisonList.size() - 1; i >= 0; i--)
        {
            if (PoisonList[i]->PType != PoisonType::LRParalysis)
            {
                continue;
            }

            PoisonList.erase(PoisonList.begin() + i);
            OperateTime = 0;
        }

        if (Master != nullptr && Master != attacker)
        {
            if (getEnvir()->getTime() > Master->BrownTime && Master->getPKPoints() < 200)
            {
                attacker->BrownTime = getEnvir()->getTime() + Settings::Minute;
            }
        }

        if (EXPOwner == nullptr || EXPOwner->Dead)
        {
            EXPOwner = attacker;
        }

        if (EXPOwner == attacker)
        {
            EXPOwnerTime = getEnvir()->getTime() + EXPOwnerDelay;
        }

        S::ObjectStruck *tempVar = new S::ObjectStruck();
        tempVar->ObjectID = ObjectID;
        tempVar->AttackerID = attacker->ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        Broadcast(tempVar);
        attacker->GatherElement();
        ChangeHP(-1);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        return 1;
    }

    void TucsonEgg::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

    }

    void TucsonEgg::CompleteAttack(std::vector<std::any> &data)
    {
        std::vector<MapObject*> targets = FindAllTargets(1, getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
                return;
            }

            if (targets[i]->Attacked(this, damage, DefenceType::MAC) <= 0)
            {
                return;
            }

            PoisonTarget(targets[i], 3, 5, PoisonType::Green, 2000);
        }
    }

    void TucsonEgg::Die()
    {
        DelayedAction tempVar(DelayedType::Damage, getEnvir()->getTime() + 300);
        ActionList.push_back(&tempVar);

        if (Info->Effect == 1)
        {
            SpawnSlave();
        }

        MonsterObject::Die();
    }

    void TucsonEgg::SpawnSlave()
    {
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        MonsterObject *mob = nullptr;
        mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::TucsonGeneralEgg));

        if (mob == nullptr)
        {
            return;
        }
        if (!mob->Spawn(getCurrentMap(), getFront()))
        {
            mob->Spawn(getCurrentMap(), getCurrentLocation());
        }

        mob->setTarget(getTarget());
        mob->ActionTime = getEnvir()->getTime() + 2000;
        SlaveList.push_back(mob);
    }
}
